/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unicam.fishpass.prism;

/**
 * Takes into consideration all the guards (all the invariants that have to be
 * verified to go on in the compilation of the code)
 * 
 * @author Andrea Baiocco
 */
public class Guard {

	private BooleanExpression bExpr;
	private ConstraintExpression cExpr;
	private Guard right;
	private Guard left;

	/**
	 * @param expr
	 */
	public Guard(BooleanExpression expr) {
		this.bExpr = expr;
		cExpr = null;
		right = null;
		left = null;
	}

	/**
	 * @param expr
	 */
	public Guard(ConstraintExpression expr) {
		bExpr = null;
		this.cExpr = expr;
		right = null;
		left = null;
	}

	/**
	 * @param left
	 * @param right
	 */
	public Guard(Guard left, Guard right) {
		this.left = left;
		this.right = right;
		bExpr = null;
		cExpr = null;
	}

	boolean evaluateGuard(Declaration[] decs, int[] clocks, float[] vars) {
		if (bExpr != null)
			return bExpr.evaluateExpression(decs, clocks, vars);
		else if (cExpr != null)
			return cExpr.evaluateConstraint(decs, clocks, vars);
		else
			return left.evaluateGuard(decs, clocks, vars)
					&& right.evaluateGuard(decs, clocks, vars);
	}

	@Override
	public String toString() {
		if (bExpr != null)
			return bExpr.toString();
		else if (cExpr != null)
			return cExpr.toString();
		else
			return left.toString() + " && " + right.toString();
	}

}
